package com.guli.mall.wms.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.constant.WareConstant;
import com.guli.common.to.LockStockInfoTo;
import com.guli.common.to.StockWareTo;
import com.guli.mall.wms.entity.WareOrderTaskEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.mall.wms.mapper.WareOrderTaskDetailMapper;
import com.guli.mall.wms.entity.WareOrderTaskDetailEntity;
import com.guli.mall.wms.service.WareOrderTaskDetailService;
import org.springframework.transaction.annotation.Transactional;


@Service("wareOrderTaskDetailService")
@Slf4j
public class WareOrderTaskDetailServiceImpl extends ServiceImpl<WareOrderTaskDetailMapper, WareOrderTaskDetailEntity> implements WareOrderTaskDetailService {

    @Override
    public PageUtils<WareOrderTaskDetailEntity> queryPage(Integer currentPage, Integer size) {

        Page<WareOrderTaskDetailEntity> page = new Page<>(currentPage, size);

        Page<WareOrderTaskDetailEntity> resultPage = this.page(page);


        return new PageUtils<>(resultPage);
    }


    @Override
    public Boolean updateWareOrderTaskDetailStatusByIdAndOldStatus(Long wareOrderTaskDetailEntityId, WareConstant.LockStatus oldStatus, WareConstant.LockStatus newStatus) {
        WareOrderTaskDetailEntity wareOrderTaskDetailEntity = new WareOrderTaskDetailEntity();

        wareOrderTaskDetailEntity.setLockStatus(newStatus);

        QueryWrapper<WareOrderTaskDetailEntity> wrapper = new QueryWrapper<>();

        wrapper.eq("id", wareOrderTaskDetailEntityId);

        wrapper.eq("lock_status", oldStatus.getCode());

        return this.update(wareOrderTaskDetailEntity, wrapper);
    }

    @Transactional
    @Override
    public void updateStockLockStatus(List<LockStockInfoTo> lockStockInfoTos, WareConstant.LockStatus status) {
        for (LockStockInfoTo lockStockInfoTo : lockStockInfoTos) {

            WareOrderTaskDetailEntity wareOrderTaskDetailEntity = new WareOrderTaskDetailEntity();

            wareOrderTaskDetailEntity.setId(lockStockInfoTo.getWareOrderTaskDetailEntityId());

            wareOrderTaskDetailEntity.setLockStatus(status);

            this.updateById(wareOrderTaskDetailEntity);
        }
    }

    @Override
    public List<WareOrderTaskDetailEntity> getByOrderTaskId(Long wareOrderTaskEntityId) {

        QueryWrapper<WareOrderTaskDetailEntity> wrapper = new QueryWrapper<>();

        wrapper.eq("task_id", wareOrderTaskEntityId);

        return this.list(wrapper);
    }

    @Transactional
    @Override
    public boolean updateMultipleWareOrderTaskDetailStatusByIdAndOldStatus(List<Long> wareOrderTaskDetailIds, WareConstant.LockStatus oldStatus, WareConstant.LockStatus newStatus) {

        WareOrderTaskDetailEntity detailEntity = new WareOrderTaskDetailEntity();

        detailEntity.setLockStatus(newStatus);

        QueryWrapper<WareOrderTaskDetailEntity> wrapper = new QueryWrapper<>();

        wrapper.eq("lock_status",oldStatus);

        wrapper.in("id",wareOrderTaskDetailIds);

        return this.update(detailEntity,wrapper);
    }


}